package de.velvettear.squirrel.gui;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.executors.SecurityCamExecutor;
import de.velvettear.squirrel.kernel.Statics;
import de.velvettear.squirrel.util.CameraHelper;

public class SecurityCamFrame extends Activity implements OnClickListener {

	private boolean isRunning = false;
	
//	private CameraPreview preview;	
	private Button btn_startstop;
	private ImageView preview;
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_securitycam);
	    
//	    preview = new CameraPreview(SecurityCamFrame.this, this);
//	    
//	    LinearLayout ll_preview = (LinearLayout) findViewById(R.id.securitycam_ll_preview);
//	    ll_preview.addView(preview);
	    
	    btn_startstop = (Button) findViewById(R.id.securitycam_btn_startstop);
	    btn_startstop.setOnClickListener(this);
	    
	    preview = (ImageView) findViewById(R.id.securitycam_iv_preview);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
        int rotation = manager.getDefaultDisplay().getRotation(); 
        switch (rotation) {
        case Surface.ROTATION_0:
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        	break;
        case Surface.ROTATION_90:
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        	break;
        case Surface.ROTATION_180:
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        	break;
        case Surface.ROTATION_270: 
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        	break;     
        }
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.securitycam_btn_startstop:			
			if (isRunning) {
				isRunning = false;
				btn_startstop.setText(R.string.button_start);
				SecurityCamExecutor.stopTakingPictures();
				Toast.makeText(this, "SecurityCam stopped!", Toast.LENGTH_SHORT).show();
			} else {
				isRunning = true;
				btn_startstop.setText(R.string.button_stop);
				SecurityCamExecutor.startTakingPictures(this, 30, Statics.SECURITYCAM_MODE_FTP, "SecurityCam", preview);
				Toast.makeText(this, "SecurityCam started!", Toast.LENGTH_SHORT).show();
			}			
			break;
		
		}		
	}
			
	@Override
	public void onDestroy() {		
		SecurityCamExecutor.stopTakingPictures();
		CameraHelper.resetCamera();
		
		super.onDestroy();
	}
	
}
